package com.myhome.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.myhome.domain.RolePrivilegesParam;
import com.myhome.domain.SysRolePrivilege;
import com.myhome.service.SysRolePrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myhome.mapper.SysPrivilegeMapper;
import com.myhome.domain.SysPrivilege;
import com.myhome.service.SysPrivilegeService;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
*@Author QYiXin 
* @Date 2021 10 09 12 58
**/
@Service
public class SysPrivilegeServiceImpl extends ServiceImpl<SysPrivilegeMapper, SysPrivilege> implements SysPrivilegeService{
    @Autowired
    SysRolePrivilegeService sysRolePrivilegeService;
    @Override
    public List<SysPrivilege> getPrivileges(Long roleId, Long menuId) {
        List<SysPrivilege> privilegesList = this.list(new QueryWrapper<SysPrivilege>().like(
                "menu_id",
                menuId
        ));
        if (CollectionUtil.isEmpty(privilegesList)) {
            return Collections.emptyList();
        }
        List<SysRolePrivilege> sysRolePrivilegeList = sysRolePrivilegeService.list(new QueryWrapper<SysRolePrivilege>().eq("role_id", roleId));
        List<Long> privilegeIdList = sysRolePrivilegeList.stream().map(a -> a.getPrivilegeId()).collect(Collectors.toList());
        for (SysPrivilege sysPrivilege : privilegesList) {
            if (privilegeIdList.contains(sysPrivilege.getId())) {
                sysPrivilege.setOwn(1);
            }
        }
        return privilegesList;
    }

}
